<template>
  <wd-popup
    v-model="showPlaylist"
    position="center"
    custom-style="width:80%;height:70%;background-color:#1c1c1e;"
  >
    <view class="playlist-container">
      <view class="playlist-tabs">
        <scroll-view scroll-x class="tabs-scroll">
          <view class="tabs-container">
            <view
              v-for="(tab, index) in tabs"
              :key="index"
              class="tab-item"
              :class="{ active: currentPage === index }"
              @click="switchPage(index)"
            >
              <text>{{ tab.label }}</text>
              <view v-if="currentPage === index" class="tab-indicator"></view>
            </view>
          </view>
        </scroll-view>
      </view>

      <wd-loading v-if="loading" class="loading" />

      <scroll-view scroll-y class="playlist-scroll">
        <text class="playlist-title">播放列表 ({{ data.total || 0 }})</text>
        <view
          v-for="(item, index) in data.list"
          :key="index"
          class="playlist-item"
          :class="{ active: playingItem?.id === item.id }"
          @click="playItem(item)"
        >
          <text class="item-title">{{ item.title }}</text>
          <text class="item-duration">{{ formatDuration(item.duration) }}</text>
          <wd-icon v-if="playingItem?.id === item.id" name="volume" size="24rpx" color="#5b8eff" />
        </view>
      </scroll-view>
    </view>
  </wd-popup>
</template>

<script lang="ts" setup>
import type { MediaView } from '@/api/children.typing'

export interface PlaylistTab {
  label: string
  startIndex: number
  endIndex: number
}

defineProps({
  data: {
    type: Object,
    default: () => ({ list: [], total: 0 }),
  },
  tabs: Array as () => PlaylistTab[],
  currentPage: Number,
  playingItem: Object,
  loading: Boolean,
  formatDuration: Function,
})

const showPlaylist = defineModel<boolean>('showPlaylist', { required: true, default: false })

const emit = defineEmits(['update:show', 'play', 'switch-page'])

const switchPage = (index: number) => emit('switch-page', index)
const playItem = (item: MediaView) => {
  emit('play', item)
  emit('update:show', false)
}
</script>

<style scoped lang="scss">
.playlist-container {
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;

  .loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 10;
  }

  .playlist-title {
    display: block;
    font-size: 36rpx;
    color: #fff;
    font-weight: 600;
    margin: 20rpx 0;
    padding-bottom: 15rpx;
    border-bottom: 1px solid #333;
  }

  .playlist-scroll {
    flex: 1;
    padding: 0 30rpx;
    max-height: 70vh;
  }

  .playlist-item {
    display: flex;
    align-items: center;
    padding: 25rpx 15rpx;
    border-bottom: 1px solid #333;
    transition: background-color 0.3s;

    &.active {
      background-color: rgba(91, 142, 255, 0.1);
      animation: playingPulse 2s infinite;

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 6rpx;
        background: #5b8eff;
      }
    }
  }

  .item-title {
    flex: 1;
    font-size: 32rpx;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .item-duration {
    font-size: 28rpx;
    color: #999;
    margin: 0 20rpx;
  }

  .playlist-tabs {
    position: sticky;
    top: 0;
    z-index: 10;
    background-color: #1c1c1e;
    border-bottom: 1rpx solid #333;
    padding-bottom: 10rpx;

    .tabs-scroll {
      white-space: nowrap;
      width: 100%;
      height: 80rpx;
      overflow-x: auto;
      -webkit-overflow-scrolling: touch;

      .tabs-container {
        display: inline-flex;
        height: 100%;
      }
    }

    .tab-item {
      position: relative;
      padding: 0 30rpx;
      height: 100%;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 28rpx;
      color: #999;
      transition: color 0.3s;

      &.active {
        color: #fff;
        font-weight: 500;
      }

      .tab-indicator {
        position: absolute;
        bottom: -10rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 40rpx;
        height: 4rpx;
        background-color: #5b8eff;
        border-radius: 2rpx;
      }
    }
  }
}

@keyframes playingPulse {
  0% {
    opacity: 0.5;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0.5;
  }
}
</style>
